import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    books: [
      { id: 1, title: "Python编程", author: "John Doe", copies: 4 },
      { id: 2, title: "历史的秘密", author: "Jane Smith", copies: 3 },
      { id: 3, title: "宇宙探索", author: "Emily Brown", copies: 1 },
      { id: 4, title: "健康生活", author: "Mike Johnson", copies: 8 },
      { id: 5, title: "数学之美", author: "Sarah Lee", copies: 49 },
      { id: 6, title: "艺术欣赏", author: "David White", copies: 7 },
      { id: 7, title: "编程入门", author: "Alice Green", copies: 3 },
      { id: 8, title: "科幻世界", author: "Frank Black", copies: 1 },
      { id: 9, title: "自然奇观", author: "Grace Blue", copies: 8 },
      { id: 10, title: "音乐之声", author: "Tom Red", copies: 6 }
    ],  
    tableData: [{
      id: 1,
      title: "Python编程", 
      readerName: '王五' ,
      borrowDate:'2024/6/5',
      deadline:'2024/8/5',
    }, {
      id: 2,
      title: "历史的秘密",
      readerId: 'zhangsan',  
      readerName: '张三' ,
      borrowDate:'2024/6/8 ',
      deadline:'2024/8/8',
    }, {
      id: 3,
      title: "宇宙探索",
      readerId: 'zhaoliu',  
      readerName: '赵六' ,
      borrowDate:'2024/5/3 ',
      deadline:'2024/8/3',
    }, {
      id: 4,
      title: "健康生活",
      readerId: 'zhouer',  
      readerName: '周二' ,
      borrowDate:'2024/5/3 ',
      deadline:'2024/8/3',
    },{
      id: 5,
      title: "数学之美",
      readerId: 'sunqi',  
      readerName: '孙七' ,
      borrowDate:'2024/5/13 ',
      deadline:'2024/8/13',
    },{
      id: 6,
      title:  "艺术欣赏",
      readerId: 'maliu',  
      readerName: '马六' ,
      borrowDate:'2024/6/3 ',
      deadline:'2024/8/3',
    }], 
    
    search: ''
  },
  mutations: {
    BORROW_BOOK(state, bookId) {
      const book = state.books.find(b => b.id === bookId);
      if (book) {
        book.copies--;
      }
    },
    RETURN_BOOK(state, bookId) {
      const book = state.books.find(b => b.id === bookId);
      if (book) {
        book.copies++; // 增加副本数量
      }
    },
    ADD_BORROWED_BOOK(state, book) {
      state.tableData.push(book);
    },
    SET_SEARCH(state, value) {
      state.search = value;
    },
    ADD_BOOK(state, newBook) {
      state.books.push(newBook);
    },
    UPDATE_BOOK(state, updatedBook) {
      const index = state.books.findIndex(book => book.id === updatedBook.id);
      if (index !== -1) {
        Vue.set(state.books, index, updatedBook);
      }
    },
    DELETE_BOOK(state, bookId) {
      state.books = state.books.filter(book => book.id !== bookId);
    }
  },
  actions: {
    borrowBook({ commit }, bookId) {
      commit('BORROW_BOOK', bookId);
    },
    returnBook({ commit }, bookId) {
      commit('RETURN_BOOK', bookId); // 直接提交mutation
      // 如果有异步逻辑（如调用后端API），需在这里处理并确保在成功后再commit mutation
    },
    addBorrowedBook({ commit }, book) {
      commit('ADD_BORROWED_BOOK', book);
    },
    addBook({ commit }, book) {
      commit('ADD_BOOK', book);
    },
    updateBook({ commit }, book) {
      commit('UPDATE_BOOK', book);
    },
    deleteBook({ commit }, bookId) {
      commit('DELETE_BOOK', bookId);
    },
    setSearch({ commit }, value) {
      commit('SET_SEARCH', value);
    }
  },
  getters: {
    availableBooks: state => state.books.filter(book => book.copies > 0),
    filteredBooks: state => {
      return state.books.filter(book => !state.search || book.title.toLowerCase().includes(state.search.toLowerCase()));
    }
  }
});